home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / plain C OS8 / Gadgets / EditText.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-29  |  9.8 KB  |  436 lines  |  [TEXT/CWIE]

  1. /* EditText.c */
  2.  
  3. #include <Types.h>
  4. #include <Quickdraw.h>
  5. #include <Controls.h>
  6. #include <Dialogs.h>
  7. #include <Events.h>
  8. #include <Lists.h>
  9. #include <Menus.h>
  10. #include <Resources.h>
  11. #include <Sound.h>
  12. #include <TextEdit.h>
  13. #include <ToolUtils.h>
  14. #include <Appearance.h>
  15. #include <stdlib.h>
  16.  
  17. #include "Globals.h"
  18. #include "ResourceDefs.h"
  19. #include "DoScrap.h"
  20. #include "Miscellany.h"
  21. #include "Scrolling.h"
  22. #include "WindowAids.h"
  23. #include "ControlUtils.h"
  24. #include "Dispatcher.h"
  25. #include "DDocData.h"
  26. #include "GadgetsEngine.h"
  27. #include "GadgetsDoc.h"
  28. #include "EditText.h"
  29.  
  30.  
  31. static    GadgetsEngine*      GetEngine    (EditText*    self);
  32.  
  33. static    void    DoUndo        (EditText*    self);
  34. static    void    DoCut        (EditText*    self);
  35. static    void    DoCopy        (EditText*    self);
  36. static    void    DoPaste        (EditText*    self);
  37. static    void    DoClear        (EditText*    self);
  38. static    void    DoSelectAll        (EditText*    self);
  39. static    void    DoShowClipboard    (EditText*    self);
  40.  
  41.  
  42. //----------
  43. EditText*        NewEditText ()
  44. {
  45.     EditText*        window;
  46.  
  47.     window = (EditText*)malloc (sizeof (EditText));
  48.     EditText_Init (window);
  49.     SetClassID (window, classEditText);
  50.  
  51.     return window;
  52. }
  53.  
  54. //----------
  55. void    DeleteEditText (
  56.     EditText*        window)
  57. {
  58.     EditText_Free (window);
  59.     free (window);
  60. }
  61.  
  62. //----------
  63. void    EditText_Create (
  64.     AMDoc*            inDoc,
  65.     DDocData*        inData)
  66. {
  67.     EditText*        winObj = NewEditText ();
  68.  
  69.     if (winObj != nil) {
  70.         EditText_Open (winObj, inDoc, inData);
  71.     }
  72. }
  73.  
  74. //----------
  75. void    EditText_Init (
  76.     EditText*        self)
  77. {
  78. }
  79.  
  80. //----------
  81. void    EditText_Free (
  82.     EditText*        self)
  83. {
  84. }
  85.  
  86. //----------
  87. GadgetsEngine*    GetEngine (
  88.     EditText*        self)
  89. {
  90.     return (GadgetsEngine*) self->super.mDoc->mEngine;
  91. }
  92.  
  93. /*----------*/
  94. void    EditText_Open (
  95.     EditText*        self,
  96.     AMDoc*            inDoc,
  97.     DDocData*        inData)
  98. {
  99.     WindowPtr        window;
  100.     Handle            wftb;
  101.  
  102.     self->super.mDoc = inDoc;
  103.     self->mData = inData;
  104.     AddResponder ((AMSignaler*) self->mData, (AMResponder*) self);
  105.  
  106.     window = GetWindow (WIND_EditText);
  107.     if (AMEngine_GetFilename (self->super.mDoc->mEngine) [0] != 0) {
  108.         SetWTitle (window, AMEngine_GetFilename (self->super.mDoc->mEngine));
  109.     }
  110.     self->super.mWindow = window;
  111.     ((GadgetsDoc*)self->super.mDoc)->mEditTextPtr = window;
  112.  
  113.     SetWindowKind (window, 'AM');
  114.     SetWRefCon (window, (long) self);
  115.     SetPort (window);
  116.     SetInfo (window);
  117.  
  118.     wftb = GetResource ('Wftb', WIND_EditText);
  119.  
  120.     CreateRootControl (window, &self->super.mRootControl);
  121.  
  122.     self->super.vScroll = nil;
  123.     self->super.hScroll = nil;
  124.  
  125.  
  126.     self->mSmallHandle = GetNewControl (CNTL_Small, window);
  127.     SetWindowItemFont (self->mSmallHandle, wftb, 1);
  128.     SetControlTextStr (self->mSmallHandle, GetSmall (self->mData));
  129.  
  130.     self->mLargeHandle = GetNewControl (CNTL_Large, window);
  131.     SetWindowItemFont (self->mLargeHandle, wftb, 2);
  132.     SetControlTextStr (self->mLargeHandle, GetLarge (self->mData));
  133.  
  134.     self->mX12345Handle = GetNewControl (CNTL_X12345, window);
  135.     SetWindowItemFont (self->mX12345Handle, wftb, 3);
  136.     SetControlTextValue (self->mX12345Handle, GetX12345 (self->mData));
  137.  
  138.     self->mX12345e6Handle = GetNewControl (CNTL_X12345e6, window);
  139.     SetWindowItemFont (self->mX12345e6Handle, wftb, 4);
  140.     SetControlTextFloat (self->mX12345e6Handle, GetX12345e6 (self->mData));
  141.  
  142.     self->mPasswordHandle = GetNewControl (CNTL_Password, window);
  143.     SetWindowItemFont (self->mPasswordHandle, wftb, 5);
  144.     SetControlTextStr (self->mPasswordHandle, GetPassword (self->mData));
  145.  
  146.     self->mDateHandle = GetNewControl (CNTL_Date, window);
  147.     SetWindowItemFont (self->mDateHandle, wftb, 6);
  148.     SetClockDateTime (self->mDateHandle, GetTheDate (self->mData));
  149.  
  150.     self->mTimeHandle = GetNewControl (CNTL_Time, window);
  151.     SetWindowItemFont (self->mTimeHandle, wftb, 7);
  152.     SetClockDateTime (self->mTimeHandle, GetTheTime (self->mData));
  153.  
  154.     self->mStyledHandle = GetNewControl (CNTL_Styled, window);
  155.     SetWindowItemFont (self->mStyledHandle, wftb, 8);
  156.     SetControlTextStr (self->mStyledHandle, GetStyled (self->mData));
  157.  
  158.     AdvanceKeyboardFocus (window);
  159.  
  160.     ShowWindow (window);
  161. }
  162.  
  163. /*----------*/
  164. void    EditText_Close (
  165.     EditText*        self)
  166. {
  167.     RemoveResponder ((AMSignaler*) self->mData, (AMResponder*) self);
  168.  
  169.     ((GadgetsDoc*)self->super.mDoc)->mEditTextPtr = nil;
  170.     SetInfo (nil);
  171.     HideWindow (self->super.mWindow);
  172.     DisposeWindow (self->super.mWindow);
  173.  
  174.     DeleteEditText (self);
  175. }
  176.  
  177. /*----------*/
  178. void    EditText_Track (
  179.     EditText*        self,
  180.     ControlHandle    whichControl,
  181.     short            whichPart,
  182.     Point            where)
  183. {
  184.     Rect            bounds;
  185.     short            newValue;
  186.  
  187.     if (whichControl == self->mSmallHandle) {
  188.         AMWindow_HandleEditClick ((AMWindow*) self, self->mSmallHandle, where);
  189.     }
  190.     if (whichControl == self->mLargeHandle) {
  191.         AMWindow_HandleEditClick ((AMWindow*) self, self->mLargeHandle, where);
  192.     }
  193.     if (whichControl == self->mX12345Handle) {
  194.         AMWindow_HandleEditClick ((AMWindow*) self, self->mX12345Handle, where);
  195.     }
  196.     if (whichControl == self->mX12345e6Handle) {
  197.         AMWindow_HandleEditClick ((AMWindow*) self, self->mX12345e6Handle, where);
  198.     }
  199.     if (whichControl == self->mPasswordHandle) {
  200.         AMWindow_HandleEditClick ((AMWindow*) self, self->mPasswordHandle, where);
  201.     }
  202.     if (whichControl == self->mDateHandle) {
  203.         AMWindow_HandleEditClick ((AMWindow*) self, self->mDateHandle, where);
  204.     }
  205.     if (whichControl == self->mTimeHandle) {
  206.         AMWindow_HandleEditClick ((AMWindow*) self, self->mTimeHandle, where);
  207.     }
  208.     if (whichControl == self->mStyledHandle) {
  209.         AMWindow_HandleEditClick ((AMWindow*) self, self->mStyledHandle, where);
  210.     }
  211. }
  212.  
  213. //----------
  214. void    EditText_DataChanged (
  215.     EditText*        self,
  216.     long            inDataID)
  217. {
  218.     if (inDataID == idSmall) {
  219.         SetControlTextStr (self->mSmallHandle, GetSmall (self->mData));
  220.     }
  221.     if (inDataID == idLarge) {
  222.         SetControlTextStr (self->mLargeHandle, GetLarge (self->mData));
  223.     }
  224.     if (inDataID == idX12345) {
  225.         SetControlTextValue (self->mX12345Handle, GetX12345 (self->mData));
  226.     }
  227.     if (inDataID == idX12345e6) {
  228.         SetControlTextFloat (self->mX12345e6Handle, GetX12345e6 (self->mData));
  229.     }
  230.     if (inDataID == idPassword) {
  231.         SetControlTextStr (self->mPasswordHandle, GetPassword (self->mData));
  232.     }
  233.     if (inDataID == idTheDate) {
  234.         SetClockDateTime (self->mDateHandle, GetTheDate (self->mData));
  235.     }
  236.     if (inDataID == idTheTime) {
  237.         SetClockDateTime (self->mTimeHandle, GetTheTime (self->mData));
  238.     }
  239.     if (inDataID == idStyled) {
  240.         SetControlTextStr (self->mStyledHandle, GetStyled (self->mData));
  241.     }
  242. }
  243.  
  244. /*----------*/
  245. void    EditText_MouseIn (
  246.     EditText*        self,
  247.     Point            where,
  248.     short            modifiers)
  249. {
  250.     Rect        bounds;
  251.  
  252. }
  253.  
  254. //----------
  255. void    EditText_ExitCurField (
  256.     EditText*        self)
  257. {
  258.     ControlHandle    focus;
  259.  
  260.     GetKeyboardFocus (self->super.mWindow, &focus);
  261.  
  262.     if (focus == nil) {
  263.         // nothing to exit
  264.  
  265.     } else if (focus == self->mSmallHandle) {
  266.         SetSmallHandle (self->mData, GetEditTextChars (self->mSmallHandle));
  267.     } else if (focus == self->mLargeHandle) {
  268.         SetLargeHandle (self->mData, GetEditTextChars (self->mLargeHandle));
  269.     } else if (focus == self->mX12345Handle) {
  270.         SetX12345 (self->mData, GetControlTextValue (self->mX12345Handle));
  271.     } else if (focus == self->mX12345e6Handle) {
  272.         SetX12345e6 (self->mData, GetControlTextFloat (self->mX12345e6Handle));
  273.     } else if (focus == self->mPasswordHandle) {
  274.         SetPasswordHandle (self->mData, GetEditTextChars (self->mPasswordHandle));
  275.     } else if (focus == self->mDateHandle) {
  276.         SetTheDate (self->mData, GetClockDateTime (self->mDateHandle));
  277.     } else if (focus == self->mTimeHandle) {
  278.         SetTheTime (self->mData, GetClockDateTime (self->mTimeHandle));
  279.     } else if (focus == self->mStyledHandle) {
  280.         SetStyledHandle (self->mData, GetEditTextChars (self->mStyledHandle));
  281.     }
  282. }
  283.  
  284. /*----------*/
  285. void    EditText_TypeIn (
  286.     EditText*        self,
  287.     char            ch)
  288. {
  289.     ControlHandle    focus;
  290.     short            keyCode;
  291.  
  292.     GetKeyboardFocus (self->super.mWindow, &focus);
  293.  
  294.     if ((ch == charEnter)
  295.     ||  (ch == charReturn)) {
  296.         EditText_ExitCurField (self);    // Dispatch
  297.     } else if (ch == charEsc) {
  298.     } else if (ch == charTab) {
  299.         AMWindow_DoTab ((AMWindow*) self, (curEvent.modifiers & optionKey) != 0);
  300.     } else if (focus != nil) {
  301.         keyCode = curEvent.message & keyCodeMask;
  302.         HandleControlKey (focus, keyCode, ch, curEvent.modifiers);
  303.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  304.     } else {
  305.         SysBeep (1);
  306.     }
  307. }
  308.  
  309. /*----------*/
  310. void    EditText_Resize (
  311.     EditText*        self)
  312. {
  313.     /* application-specific code to resize items in window */
  314. }
  315.  
  316. /*----------*/
  317. void    EditText_Scroll (
  318.     EditText*        self,
  319.     short            newValue,
  320.     short            oldValue)
  321. {
  322.     /* application-specific code to scroll window */
  323.     if (gWhichScroll == self->super.vScroll) {
  324.     } else {    // horizontal
  325.     }
  326. }
  327.  
  328. //----------
  329. void    DoUndo (
  330.     EditText*        self)
  331. {
  332. } // DoUndo
  333.  
  334. //----------
  335. void    DoCut (
  336.     EditText*        self)
  337. {
  338.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  339.  
  340.     if (curTE != nil) {
  341.         TECut (curTE);
  342.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  343.         scrapDirty = true;
  344.     }
  345. } // DoCut
  346.  
  347. //----------
  348. void    DoCopy (
  349.     EditText*        self)
  350. {
  351.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  352.  
  353.     if (curTE != nil) {
  354.         TECopy (curTE);
  355.         scrapDirty = true;
  356.     }
  357. } // DoCopy
  358.  
  359. //----------
  360. void    DoPaste (
  361.     EditText*        self)
  362. {
  363.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  364.  
  365.     if (curTE != nil) {
  366.         TEPaste (curTE);
  367.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  368.     }
  369. } // DoPaste
  370.  
  371. //----------
  372. void    DoClear (
  373.     EditText*        self)
  374. {
  375.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  376.  
  377.     if (curTE != nil) {
  378.         TEDelete (curTE);
  379.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  380.     }
  381. } // DoClear
  382.  
  383. //----------
  384. void    DoSelectAll (
  385.     EditText*        self)
  386. {
  387.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  388.  
  389.     if (curTE != nil) {
  390.         TESetSelect (0, 32767, curTE);
  391.     }
  392. } // DoSelectAll
  393.  
  394. //----------
  395. void    DoShowClipboard (
  396.     EditText*        self)
  397. {
  398. } // DoShowClipboard
  399.  
  400. //----------
  401. Boolean        EditText_DoCommand (
  402.     EditText*        self,
  403.     long            inCommand)
  404. {
  405.     Boolean        result = true;
  406.  
  407.     switch (inCommand) {
  408.         case cmdUndo:
  409.                 DoUndo (self);
  410.             break;
  411.         case cmdCut:
  412.                 DoCut (self);
  413.             break;
  414.         case cmdCopy:
  415.                 DoCopy (self);
  416.             break;
  417.         case cmdPaste:
  418.                 DoPaste (self);
  419.             break;
  420.         case cmdClear:
  421.                 DoClear (self);
  422.             break;
  423.         case cmdSelectAll:
  424.                 DoSelectAll (self);
  425.             break;
  426.         case cmdShowClipboard:
  427.                 DoShowClipboard (self);
  428.             break;
  429.  
  430.         default:
  431.                 result = false;
  432.     } // switch
  433.  
  434.     return result;
  435. }
  436.